<include file="Public/common" />
<block name="css">
    <style>

        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button{
            -webkit-appearance: none !important;
            margin: 0;
        }
        .row-2{
            padding: 0px 10px;
        }
        .row-3{
            padding: 2px;
        }

        .form-group{
            padding-right: 50px!important;
            width: 23%!important;
            margin-bottom: 15px!important;
        }
        .create-time-item{
            padding-left: 14px;
        }
        .range-item{
            padding: 7px 10px;
            background-color: #eee;
        }

        .input-left{
            margin-left: -4px;
        }
        .width-600{
            width: 600px!important;
        }
        .margin-left-7{
            margin-left: 7px!important;
        }
        .margin-left-20{
            margin-left: 20px!important;
        }
        .margin-left-28{
            margin-left: 28px!important;
        }
        .margin-left-25{
            margin-left: 25px!important;
        }
        .margin-left-40{
            margin-left: 40px!important;
        }
        .margin-left-47{
            margin-left: 47px!important;
        }
        .margin-left-48{
            margin-left: 48px!important;
        }
        ul,li{ list-style: none; }
        ol{margin:0;}
        .jedatehms li{display: none;}
        #table .btn{
            margin-bottom: 5px;
        }
    </style>
</block>
<block name="content">
    <div class="page-header"><h1>首页 > 财务管理 &gt;销售订单结算</h1></div>
    <ul id="nav" class="nav nav-tabs">
        <li role="presentation"  <if condition="$_GET['order_status'] eq 1 || !$_GET['order_status']">class="active"</if> ><a href="{:U('settlement_list?order_status=1')}">待审核</a></li>
        <li role="presentation" <if condition="$_GET['order_status'] eq 2">class="active"</if>  ><a href="{:U('settlement_list?order_status=2')}">已审核</a></li>
        <li role="presentation"  <if condition="$_GET['order_status'] eq -3">class="active"</if> ><a href="{:U('settlement_list?order_status=-3')}">待退款</a></li>
        <li role="presentation"  <if condition="$_GET['order_status'] eq -5">class="active"</if> ><a href="{:U('settlement_list?order_status=-5')}">已退款</a></li>
    </ul>
    <div class="tab-content">
        <div class="row" >
            <form action="" id="settlement-form" class="form-inline"  style="margin-left:10px;" >
                <input type="hidden" name="p" value="1"/>
                <div class="row-2" >
                    <div class="form-group margin-left-28">
                        <label for="order_num">订单号:</label>
                        <input type="text" class="" name="order_num" id="order_num" value="<if condition="$_GET['order_num']">{$_GET['order_num']}</if>">
                    </div>
                    <div class="form-group margin-left-47">
                        <label for="group_num">团号:</label>
                        <input type="text" class="" name="group_num" id="group_num" value="<if condition="$_GET['group_num']">{$_GET['group_num']}</if>">
                    </div>
                    <div class="form-group margin-left-7">
                        <label for="line_name">线路名称:</label>
                        <input type="text" class="" name="line_name" id="line_name" value="<if condition="$_GET['line_name']">{$_GET['line_name']}</if>">
                    </div>
                </div>
                <div class="row-2">
                    <div class="form-group margin-left-28">
                        <label for="order_status">分销商:</label>
                        <select name="reseller_id">
                        <option value="">全部</option>
                        <foreach name="reseller" item="v">
                            <option value="{$v['reseller_id']}" <if condition="$v['reseller_id'] == $_GET['reseller_id']">selected</if>>{$v['reseller_name']}</option>
                        </foreach>
                    </select>
                    </div>
                    <div class="form-group margin-left-20">
                        <label for="order_status">直营门店:</label>
                        <select name="shop_id">
                            <option value="">全部</option>
                            <foreach name="shop" item="v">
                                <option value="{$v['operator_id']}" <if condition="$v['operator_id'] == $_GET['shop_id']">selected</if>>{$v['store_name']}</option>
                            </foreach>
                        </select>
                    </div>

                </div>
                <div class="row-2" style="padding: 0 0 20px 15px;">
                    <div class="row">
                        <label for="create_time" class="create-time-item">申请退款时间:</label>
                        <input type="text" name="start_refuse_time" id="start" value="<if condition="$_GET['start_refuse_time']">{$_GET['start_refuse_time']}</if>">
                        <span class="range-item input-left">至</span>
                        <input class="input-left" type="text" name="end_refuse_time" id="end" value="<if condition="$_GET['end_refuse_time']">{$_GET['end_refuse_time']}</if>">
                    </div>
                </div>
                <div class="col-xs-12 " style="padding:0 15px 15px 15px">
                    <a href="{:U('outSettlementListExportExcel?',['order_status'=>$_GET['order_status']])}" class="btn btn-primary">导出</a>
                    <button type="submit" class="btn btn-primary submit-btn">查询</button>
                    <div class="dropdown" style="display: inline-block">
                    <button type="button" class="btn btn-primary dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown">筛选<span class="caret"></span></button>
                    <ul id="menuList" class="dropdown-menu pull-right" role="menu" aria-labelledby="dropdownMenu1" style="padding:10px;"></ul>
                </div>
                </div>
            </form>
        </div>
        <table id="table" class="table table-striped table-bordered table-hover table-condensed mt-15">
            <thead>
                <tr>
                    <th >订单信息</th>
                    <th>预订人数</th>
                    <th>门店/分销</th>
                    <th>销售单价</th>
                    <th>销售人</th>
                    <th>结算总金额</th>
                    <th>退款金额</th>
                    <th><if condition="$_GET['order_status'] == -5">退款时间<else/>申请退款时间</if></th>
                    <th>操作人</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <volist name="list" id="v" key="i">
                    <tr>
                        <td>
                            订单号：{$v.order_num}<br>
                            团号：{$v.group_num}<br>
                            线路名称：{$v.line_name}
                            <if condition ="$v['order_type'] eq 1">(自营)
                            <elseif condition="$v['order_type'] eq 2"/>(分销商)
                            </if>
                        </td>
                        <td>
                            成人：{$v.adult_num}<br>
                            儿童：{$v.child_num}<br>
                           <!-- 老人：{$v.oldMan_num}<br>-->
                            合计：{$v.total_num}人
                        </td>
                        <td>
                            <if condition="$v['store']">
                                {$v['store']}(门店)
                            <else/>
                                {$v['reseller']}(分销)
                            </if>
                        </td>
                        <!-- <td></td> -->
                        <td>
                            成人：{$v['adult_price']}<br>
                            儿童：{$v['child_price']}<br>
                            <!--老人：{$v['oldman_price']}<br>-->
                        </td>
                        <td>{$v['sales']}</td>
                        <td>{$v.total_money}</td>
                        <td>{$v['back_money']}</td>
                        <td><if condition="$_GET['order_status'] == -5">{$v['refund_record_time']}<else/>{$v['refund_time']}</if></td>
                        <td>{$v['operation']}</td>
                        <td>
                            <if condition="$v['order_status'] == -3"><button type="submit" onclick="confirmRefund({$v['order_id']}, '{$v['order_num']}')" class="btn btn-primary btn-xs">确认退款</button>
                            <button type="submit" onclick="refuseRefund({$v['order_id']}, '{$v['order_num']}')" class="btn btn-primary btn-xs">拒绝退款</button>
                            </if>
                            <a  class="btn btn-primary btn-xs" onclick="openDetail('{:U('Index/orderDetail',array('order_id'=>$v['order_id']))}')" >详情</a>
                        </td>
                    </tr>
                </volist>
            </tbody>
        </table>
    </div>
    <div class="modal fade" id="bjy-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times;</button>
                    <h4 class="modal-title" id="myModalLabel"> 外币结算</h4></div>
                <div class="modal-body">
                    <form id="bjy-form" class="form-inline" action="{:U('Finance/singleForeignSettlement')}" method="post">
                        <input type="hidden" name="order_id" id="order_id">
                        <table class="table table-striped table-bordered table-hover table-condensed">
                            <tr>
                                <th width="17%">结算金额：</th>
                                <td><span class="total_money"></span></td>
                            </tr>
                            <tr>
                                <th>汇率：</th>
                                <td><input class="input-medium" type="text" id="settlement_rate" name="settlement_rate" placeholder="请输入汇率"></td>
                            </tr>
                            <tr>
                                <th>实收金额：</th>
                                <td><input class="input-medium" type="text" id="end_foreign_money" name="end_foreign_money" value="0" placeholder="请输入实际收取的金额"><span class="currency_type"></span></td>
                            </tr>
                            <tr>
                                <th>实收人民币：</th>
                                <td><input class="input-medium" readonly type="text" name="end_need_pay"></td>
                            </tr>
                            <tr>
                                <th></th>
                                <td><input class="btn btn-success" type="submit" value="修改"></td>
                            </tr>
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
 <ul class="pagination">{$show}</ul>
</block>
<block name="js">
<script src="__JS__/jedate/jedate.js"></script>
<script>
    /**
     * 确认退款
     * @param  {[int]} oid    [订单id]
     * @param  {[string]} num [订单编号]
     */
    function confirmRefund(oid, num){
        layer.confirm('订单号：'+num,{
        btn : ['退款', '返回'],
        title: '确认退款'
        },function(){
            layer.closeAll();
            refundRequest(oid, 2);
        })
    }

    /**
     * 拒绝退款
     * @param  {[int]} oid    [订单id]
     * @param  {[string]} num [订单编号]
     */
    function refuseRefund(oid, num){
        layer.confirm('订单号：'+num,{
        btn : ['拒绝', '返回'],
        title: '拒绝退款'
        },function(){
            layer.closeAll();
            refundRequest(oid, 1);
        })
    }

    /**
     * 发送点单退款请求
     * @param  {[int]} oid [订单id]
     * @param  {[int]} type [1：通过  2：拒绝]
     */
    function refundRequest(oid, type){
        $.post('{:U('refund')}', {order_id : oid, type : type}, function(data){
            if(data.status ==1 ){
                layer.msg('操作成功', {icon: 6})
                setTimeout(function(){
                    location.reload();
                }, 1000)
            }else{
                layer.msg('操作失败', {icon: 5})
            }
        }, 'json' )
    }

    //生成筛选菜单
    (function(){
        var str = '';
        $('#table').find('tr').eq(0).find('th').each(function(i, n){
            str += '<li><input type="checkbox" class="filtrate" checked="" value="'+i+'" id="filtrate_'+i+'"><label for="filtrate_'+i+'">'+$(n).html()+'</label></li>';
        })
        $('#menuList').append(str);
    }())

    $('#menuList').on('change', '.filtrate', function(){
        var index = $(this).val();
        if($(this).prop('checked')){
            $('#table tr').each(function(i,n){  $(n).children().eq(index).show(); })
        }else{
            $('#table tr').each(function(i,n){  $(n).children().eq(index).hide();})
        }
    })

    function openDetail(url){
        layer.open({
            type: 2,
            title: '订单详情',
            area: ['100%','100%'],
            content: url
        })
    }

    /**
     * 绑定日期选择器
     * @param  {[obj]}    obj        [元素]
     * @param  {[string]} dateFormat [时间格式]
     */
    function dateFormat(obj, dateFormat){
        jeDate({
            dateCell: '#'+$(obj).attr('id'),
            format: dateFormat,
            isinitVal:false,
            isTime:true, //isClear:false,
            okfun:function(val){
            }
        });
    }
   dateFormat($('#start'), 'YYYY-MM-DD');
   dateFormat($('#end'), 'YYYY-MM-DD');

    // 全选
    $("#checkAll").click(function () {
        if($(this).is(":checked"))
        {
            $(".checkbox").each(function () {
                $(this).prop('checked',true);
            });
        }else{
            $(".checkbox").each(function () {
                $(this).prop('checked',false);
            });
        }
    });

    // 汇率绑定值变化的事件
    $("#settlement_rate").bind('input propertychange',function(){
        // console.log($this.val());
        var rate = $('#settlement_rate').val();
        var end_foreign_money=$("#end_foreign_money").val();
        var new_total_money = rate*end_foreign_money;
        // 保留两位小数
        new_total_money = new_total_money.toFixed(2);
        $("input[name='end_need_pay']").val(new_total_money);

    });
    // 外币输入值变化的事件
    $("#end_foreign_money").bind('input propertychange',function(){
        // console.log($this.val());
        var rate = $('#settlement_rate').val();
        var end_foreign_money=$("#end_foreign_money").val();
        var new_total_money = rate*end_foreign_money;
        // 保留两位小数
        new_total_money = new_total_money.toFixed(2);
        $("input[name='end_need_pay']").val(new_total_money);

    });

    // 提交切换订单状态
    function checkStatus(status) {
        $('form')[0].reset();
        $('input[name="status"]').val(status);
        $('#settlement-form').submit();
    }

    // 提交单个结算
    function goSettlement($order_id,$currency_type,$total_money){
        // alert($order_id+$currency_type);
        if ($currency_type == '人民币') {
                        //询问框
            layer.confirm('确定结算此订单吗?', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    url:'{:U("Finance/singleSettlementById")}',
                    dataType:"json",
                    type:'POST',
                    cache:false,
                    data:{order_id:$order_id},
                    success: function(data) {
                        layer.msg('结算成功');
                        if (data.status==1) {
                            setTimeout(function () {
                                location.reload();
                            },1500);
                        }
                    }
                });
            });
        }
        // 外币结算窗口
        else{
            $(".total_money").text($total_money);
            $("#order_id").val($order_id);
            $(".currency_type").text($currency_type);
            $('#bjy-edit').modal('show');

        }
    }

    // 批量结算，只能是批量结算人民币的订单 外币则需要汇率
    function batchSettlement(){
        // 批量处理
        var order_ids = '';
        $(".checkbox").each(function () {
            if($(this).is(":checked") && $(this).attr('name')=='人民币')
            {
                order_ids+=$(this).attr('id')+',';
            }
        });
        // console.log(order_ids);
        if (order_ids == '') {
            layer.alert('请勾选要结算的订单',{icon: 5});
            return;
        }
        layer.confirm('注意：只能批量结算人民币的订单！', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url:'{:U("Finance/batchSettlement")}',
                dataType:"json",
                type:'POST',
                cache:false,
                data:{order_ids:order_ids},
                success: function(data) {
                    layer.msg('结算成功');
                    if (data.status==1) {
                        setTimeout(function () {
                            location.reload();
                        },1500);
                    }
                }
            });
        });
    }

    // 批量处理
    // $(".chk_0").each(function () {
    //     if($(this).is(":checked"))
    //     {
    //         orderId+=$(this).val()+',';
    //     }
    // });
</script>
</block>
